/**
 * Class MandateUploadModule.
 * 
 * @author Maxime Ollagnier
 */
MandateUploadModule.inherits(Module);
function MandateUploadModule() {
	
	// Input file unpload
	this.inputFileModule = new InputFileModule({
		'id' : 'inputFileModule',
		'label' : I18n.get('mandate.upload'),
		'width' : 'x-large'
	});
	this.inputFileModule.getJQ();
	
	// Search form inputs
	this.inputStartDate = new InputDateModule({
		'id' : 'inputStartDate',
		'label' : I18n.get('start.date')
	});
	this.inputEndDate = new InputDateModule({
		'id' : 'inputEndDate',
		'label' : I18n.get('end.date'),
		'value' : moment().add('years', 1).format('L')
	});
	this.inputReferrer = new InputReferrerModule({
		'id' : 'inputReferrer',
		'label' : I18n.get('entity'),
		'value' : UserManager.user.referrer,
		'rootReferrer' : UserManager.user.referrer
	});
	if (Util.check(EnumManager.MonitoringTaskStatus)) {
		this.inputStatus = new InputSelectModule({
			'id' : 'inputStatus',
			'label' : I18n.get('status'),
			'options' : EnumManager.MonitoringTaskStatus
		});
	}
	
	// Result table
	this.uploadTable = this.initUploadTable();
}

MandateUploadModule.neededRoles = ['UPLOAD_MANDATE'];

MandateUploadModule.prototype.fillJQ = function(jQ) {
	var that = this;
	
	// Title
	jQ.append($('<h4>' + I18n.get('mandate.upload.title') + '</h4>'));
	
	// Mandate upload form
	var uploadForm = $('<div class="cropbottom well"></div>');
	jQ.append(uploadForm);
	var fileUpload = $('<div class="fileUpload"></div>');
	uploadForm.append(fileUpload);
	fileUpload.append(this.inputFileModule.buildJQ());
	fileUpload.append($('<span class="btn send">' + I18n.get('send') + '</span>').click(function() {
		that.uploadMandates();
	}));
	
	// Mandate search form
	jQ.append($('<h5>' + I18n.get('mandate.upload.search') + '</h5>'));
	var searchForm = $('<div class="well row-fluid"></div>');
	jQ.append(searchForm);
	
	var columnLeft = $('<div class="span6"></div>');
	columnLeft.append(this.inputStartDate.buildJQ());
	columnLeft.append(this.inputReferrer.buildJQ());
	var columnRight = $('<div class="span6"></div>');
	columnRight.append(this.inputEndDate.buildJQ());
	columnRight.append(this.inputStatus.buildJQ());
	searchForm.append(columnLeft).append(columnRight);

	// Buttons
	var buttonRow = $('<div class="btn-row"></div>');
	searchForm.append(buttonRow);
	
	var buttonSearch = $('<div class="btn search">' + I18n.get('search') + '</div>');
	buttonSearch.click(function() { that.searchUploadTasks(); });
	buttonRow.append(buttonSearch);
	
	// Mandate upload table
	var result = $('<div class="result"></div>');
	jQ.append(result);
	result.append($('<br/>'));
	result.append($('<h5>' + I18n.get('mandate.upload.list') + '</h5>'));
	if (this.uploadTable.objectMap.size == 0) {
		result.hide();
	}
	result.append(this.uploadTable.buildJQ());
	
	// Launches the search
	this.searchUploadTasks();
};

MandateUploadModule.prototype.initUploadTable = function(jQ) {
	var that = this;
	
	// Mandate upload tasks table
	uploadTable = new TableModule();
	uploadTable.id = 'uploadTable';
	
	// Start date
	var startDateCol = new Column('startDate', I18n.get('start.date'), '10%');
	uploadTable.columnList.push(startDateCol);
	startDateCol.getText = function(task) {
		return moment(task.startDate).format('DD/MM/YYYY HH:mm:ss');
	};
	
	// End date
	var endDateCol = new Column('endDate', I18n.get('end.date'), '10%');
	uploadTable.columnList.push(endDateCol);
	endDateCol.getText = function(task) {
		return (task.endDate == null | task.endDate == '')? '' : moment(task.endDate).format('DD/MM/YYYY HH:mm:ss');
	};
	
	// File name
	uploadTable.columnList.push(new Column('fileName', I18n.get('file.name'), '20%'));
	
	// Status
	var statusCol = new Column('status', I18n.get('status'), '11%');
	uploadTable.columnList.push(statusCol);
	statusCol.getText = function(task) {
		return I18n.get('MandateUploadTaskStatus.' + task.status);
	};

	// Download
	var downloadCol = new Column('download', '', '5%');
	uploadTable.columnList.push(downloadCol);
	downloadCol.getAlt = function(task) {
		return I18n.get('download');
	};
	downloadCol.getText = function(task) {
		if (true) { // TODO task.status == 'AVAILABLE') {
			return $('<i class="icon-download-alt icon"/>').click(function() { that.downloadUploadTaskReport(task.id); });
		}
		// TODO return '';
	};
	
	return uploadTable;
};

MandateUploadModule.prototype.uploadMandates = function() {
	var that = this;
	if (this.inputFileModule.value != '') {
		this.inputFileModule.submit('uploadCSVMandate', {
			'referrerId' : this.inputReferrer.value.id
		}, function(result) {
			that.reload();
		});
	}
};

MandateUploadModule.prototype.searchUploadTasks = function() {
	var that = this;
	AjaxManager.getJSON('getMandateTasks', {
		'referrerId' : this.inputReferrer.value.id,
		'startDate' : moment(this.inputStartDate.value, 'DD/MM/YYYY').valueOf(),
		'endDate' : moment(this.inputEndDate.value, 'DD/MM/YYYY').valueOf(),
		'status' : this.inputStatus.value
	}, function(result) {
		that.uploadTable.objectMap = new Map();
		if (result.success) {
			that.getJQ().find('.result:first').show();
			$.each(result.tasks, function(id, task) {
				that.uploadTable.objectMap.put(id, task);
			});
			that.uploadTable.objectMap.sort('startDate', true);
		}
		that.uploadTable.buildJQ();
	});
};

MandateUploadModule.prototype.downloadUploadTaskReport = function(taskId) {
	AjaxManager.download('downloadMandateTaskReport', {
		'taskId' : taskId
	});
};
